package com.daka.controller;

import com.daka.entity.AuctionTradeList;
import com.daka.entity.Order;
import com.daka.enumerate.StatusCode;
import com.daka.service.AuctionTradeListService;
import com.daka.service.OrderService;
import com.daka.vo.DataResponseVo;
import com.daka.vo.db.OrderInfoVo;
import com.daka.vo.wechat.CallBackVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/12/13 19:39
 */
@Controller
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Resource
    private AuctionTradeListService auctionTradeListService;

    /**
     * 创建订单
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/save", consumes = "application/json")
    public DataResponseVo<Object> save(@RequestBody Order order) {

        if (order.getMoney() == null) {
            return new DataResponseVo<>(StatusCode.COMMON_PARAM_ERROR.getCode(), StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        int a = orderService.insert(order);
        return new DataResponseVo<>(StatusCode.OK.getCode(), StatusCode.OK.getMessage());
    }

    /**
     * 微信回调生成订单信息
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/create", consumes = "application/json")
    public DataResponseVo<Object> create(@RequestBody CallBackVO param) {

        Order orderCheck = orderService.selectByTransactionId(param.getTransactionId());
        if(null != orderCheck){
            return new DataResponseVo<>(StatusCode.OK.getCode(), StatusCode.OK.getMessage());
        }

        if (param.getMoney() == null) {
            return new DataResponseVo<>(StatusCode.COMMON_PARAM_ERROR.getCode(), StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        Order order = new Order();
        if (param.getUserId() != null) {
            order.setUserId(Long.valueOf(param.getUserId()));
        }
        if (param.getProject() != null) {
            order.setProjectId(Long.valueOf(param.getProject()));
        }
        //支付中心订单uuid
        if (param.getPayCenterUuid() != null) {
            order.setPayCenterUuid(param.getPayCenterUuid());
        }
        //微信交易ID
        if (param.getTransactionId() != null) {
            order.setTransactionId(param.getTransactionId());
        }
        //实际支付金额
        if (param.getTotalFee() != null) {
            order.setTotalFee(param.getTotalFee());
        }
        if (param.getChannelCode() != null) {
            order.setChannelCode(param.getChannelCode());
        }
        if (param.getTimeEnd() != null) {
            order.setTimeEnd(param.getTimeEnd());
        }
        if (param.getOrderCode() != null) {
            order.setOrderCode(param.getOrderCode());
        }
        order.setMoney(param.getMoney());

        if("11".equalsIgnoreCase(order.getOrderCode()) && StringUtils.isNotEmpty(order.getOrderCode())){
            orderService.create(order);
        }else{
            Integer auctionId = Integer.parseInt(order.getOrderCode());
            AuctionTradeList auctionTradeList = auctionTradeListService.selectByPrimaryKey(auctionId.longValue());
            auctionTradeList.setPayStatus(1);
            auctionTradeListService.updateByPrimaryKeySelective(auctionTradeList);

            orderService.createOnly(order);

        }
        return new DataResponseVo<>(StatusCode.OK.getCode(), StatusCode.OK.getMessage());
    }

    /**
     * 订单流水、时间倒序
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/queryAll", consumes = "application/json")
    public DataResponseVo<Object> queryAll() {
        List<OrderInfoVo> list = orderService.findAll();
        return new DataResponseVo<>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(), list);
    }

}
